package rexsee.cartoon.gif;

import java.io.File;

import rexsee.core.browser.Browser;
import rexsee.core.lang.RexseeLanguage;
import rexsee.core.style.CanvasSheet;
import rexsee.core.transportation.PageSaver;
import rexsee.core.widget.DivDialog;
import android.app.Activity;
import android.content.DialogInterface;
import android.net.Uri;
import android.os.Environment;
import android.view.KeyEvent;

public class GifDialog extends DivDialog {

	public final GifView mView;
	private Runnable mClickCallback = null;

	public GifDialog(Browser browser, String style) {
		super(browser, null, "icon_scale:fitxy;" + style);
		mView = new GifView(browser.getContext());
		this.addChild(mView);
	}

	public void destroy() {
		mView.destroy();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (mBrowser.menu.isOptionsMenuOpen) {
				((Activity) mContext).closeOptionsMenu();
				return true;
			} else {
				if (styleSheet.window_cancelable.equalsIgnoreCase("true")) dismiss();
			}
		} else if (keyCode == KeyEvent.KEYCODE_MENU) {
			((Activity) mContext).openOptionsMenu();
		} else if (keyCode == KeyEvent.KEYCODE_SEARCH) {
			return true;
		} else if (keyCode == KeyEvent.KEYCODE_FOCUS) {
		} else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
		} else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
		} else {
		}
		return false;
	}

	@Override
	protected void onClick(DialogInterface dialog) {
		if (mClickCallback != null) {
			mClickCallback.run();
		}
	}

	public void setGif(final String url, final CanvasSheet style) {

		if (url == null) {
			mView.destroy();
			mView.postInvalidate();
			return;
		}

		String check = url.toLowerCase().trim();
		if (!check.endsWith(".gif")) {
			mBrowser.exception(this.getClass().getName(), "Invalid gif extension.");
			return;
		}
		if (!check.startsWith("http://") && !check.startsWith("https://") && !check.startsWith("file://")) {
			mBrowser.exception(this.getClass().getName(), "Invalid gif scheme.");
			return;
		}
		if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
			mBrowser.exception(this.getClass().getName(), "Sdcard is not ready.");
			return;
		}
		final String dir = "file://" + Environment.getExternalStorageDirectory().getAbsolutePath() + "/GifCache";
		File f = new File(Uri.parse(dir).getPath());
		if (!f.exists()) {
			if (!f.mkdirs()) {
				mBrowser.exception(this.getClass().getName(), "Creating cache dir error.");
				return;
			}
		} else {
			if (!f.isDirectory()) {
				if (!f.delete() || !f.mkdirs()) {
					mBrowser.exception(this.getClass().getName(), "Creating cache dir error.");
					return;
				}
			}
		}
		if (check.startsWith("file://")) {
			File file = new File(Uri.parse(url).getPath());
			if (!file.exists() || !file.isFile()) {
				mBrowser.exception(this.getClass().getName(), "Gif does not exist.");
				return;
			} else {
				mView.setGif(url, styleSheet.getIconScaleType(), style, new Runnable() {
					@Override
					public void run() {
						mBrowser.progressDialog.show(RexseeLanguage.PROGRESS_ONGOING);
					}
				}, new Runnable() {
					@Override
					public void run() {
						mBrowser.progressDialog.hide();
					}
				});
			}
		} else {
			final boolean cacheable = !styleSheet.mode.trim().toLowerCase().equals("nocache");
			new Thread() {
				@Override
				public void run() {
					final String path = PageSaver.download(mBrowser, dir, cacheable, url);
					if (path == null) {
						mBrowser.progressDialog.hide();
						mBrowser.exception("GifDialog", "Cache failed.");
					} else {
						((Activity) mContext).runOnUiThread(new Runnable() {
							@Override
							public void run() {
								mView.setGif(path, styleSheet.getIconScaleType(), style, new Runnable() {
									@Override
									public void run() {
										mBrowser.progressDialog.show(RexseeLanguage.PROGRESS_ONGOING);
									}
								}, new Runnable() {
									@Override
									public void run() {
										mBrowser.progressDialog.hide();
									}
								});
								mBrowser.progressDialog.hide();
							}
						});
					}
				}
			}.start();
		}

	}
	public void setClickCallback(Runnable clickCallback) {
		mClickCallback = clickCallback;
	}

}
